package com.dx.gulimallgateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author ：大雄
 * @date ：2021/11/9 20:26
 * @description： 解决跨域配置类
 */

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter crosWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //配置跨域
        CorsConfiguration corsConfig = new CorsConfiguration();

        //允许那些请求头可以跨域
        corsConfig.addAllowedHeader("*");
        //允许那些请求方式get,post,put,delete
        corsConfig.addAllowedMethod("*");
        //允许那些域名可以跨域
        corsConfig.addAllowedOrigin("*");
        //允许携带cookie跨域
        corsConfig.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfig);
        return new CorsWebFilter(source);
    }
}
